home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / irit / irit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-30  |  14.4 KB  |  448 lines

  1. /*****************************************************************************
  2. *   Main module of "Irit" - the 3d (not only polygonal) solid modeller.      *
  3. ******************************************************************************
  4. * Usage:                                     *
  5. *   Irit [-t] [-z]                                 *
  6. *                                         *
  7. * Written by:  Gershon Elber                Ver 3.0, Apr. 1990   *
  8. *****************************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include <signal.h>
  12. #include <string.h>
  13. #include "program.h"
  14. #include "config.h"
  15. #include "ctrl-brk.h"
  16. #include "dosintr.h"
  17. #include "inptprsg.h"
  18. #include "iritprsr.h"
  19. #include "objects.h"
  20. #include "windows.h"
  21. #include "iritgrap.h"
  22. #include "irit_soc.h"
  23. #ifdef __WINNT__
  24. #include <direct.h>
  25. #endif /* __WINNT__ */
  26.  
  27. #ifdef NO_CONCAT_STR
  28. static char *VersionStr = "Irit        Version 4.0, Gershon Elber,\n\
  29.     (C) Copyright 1989/90/91/92/93 Gershon Elber, Non commercial use only.";
  30. #else
  31. static char *VersionStr = "Irit            " VERSION
  32.     ",    Gershon Elber,    " __DATE__ ",   " __TIME__ "\n"
  33.     COPYRIGHT ", Non commercial use only.";
  34. #endif /* NO_CONCAT_STR */
  35.  
  36. static char *CtrlStr = "Irit [-t] [-z] [file.irt]";
  37.  
  38. char *GlblHelpFileName = "irit.hlp";
  39.  
  40. static char
  41.      *GlblPrgmHeader = "                Irit - the not only polygonal solid modeller";
  42. static char
  43.      *GlblCopyRight  =
  44. #ifdef DJGCC
  45.      COPYRIGHT ",  DJGCC " VERSION ",   " __DATE__;
  46. #else
  47. #ifdef OS2GCC
  48.      COPYRIGHT ",  OS2   " VERSION ",   " __DATE__;
  49. #else
  50. #ifdef AMIGA
  51.      COPYRIGHT ", Amiga  " VERSION ",   " __DATE__;
  52. #else
  53. #ifdef NO_CONCAT_STR
  54.      "(C) Copyright 1989/90/91/92 Gershon Elber,  Unix  Version 4.0";
  55. #else
  56.      COPYRIGHT ",  Unix  " VERSION;
  57. #endif /* NO_CONCAT_STR */
  58. #endif /* AMIGA */
  59. #endif /* OS2GCC */
  60. #endif /* DJGCC */
  61. static char
  62.     *GlblAuthorName = "                         Written by Gershon Elber";
  63.  
  64. IPObjectStruct
  65.     *GlblObjList = NULL;           /* All objects defined on system. */
  66.  
  67. char GlblCrntWorkingDir[LINE_LEN];     /* Save start CWD to recover on exit. */
  68.  
  69. FILE
  70.     *GlblLogFile = NULL;
  71.  
  72. jmp_buf GlblLongJumpBuffer;                  /* Used in error recovery. */
  73.  
  74. int
  75. #ifdef DJGCC                       /* Defaults for intr_lib. */
  76.     GlblWindowFrameWidth = 8,
  77.     GlblViewFrameColor   = INTR_COLOR_RED,
  78.     GlblViewBackColor    = INTR_COLOR_BLACK,
  79.     GlblTransFrameColor  = INTR_COLOR_GREEN,
  80.     GlblTransBackColor   = INTR_COLOR_BLACK,
  81.     GlblStatusFrameColor = INTR_COLOR_MAGENTA,
  82.     GlblStatusBackColor  = INTR_COLOR_BLACK,
  83.     GlblInputFrameColor  = INTR_COLOR_YELLOW,
  84.     GlblInputBackColor   = INTR_COLOR_BLACK,
  85.     GlblDrawHeader       = FALSE,
  86.     GlblSmoothTextScroll = TRUE,
  87.     GlblIntrSaveMethod   = INTR_SAVE_DISK,
  88.     GlblMouseSensitivity = 10,       /* Sensitivity control of mouse device. */
  89.     GlblJoystickExists   = FALSE,
  90. #endif /* DJGCC */
  91.     GlblLoadColor        = DEFAULT_LOAD_COLOR,
  92.     GlblBoolColor        = DEFAULT_BOOL_COLOR,
  93.     GlblICrvColor        = DEFAULT_ICRV_COLOR,
  94.     GlblPrimColor        = DEFAULT_PRIM_COLOR,
  95.     GlblDoGraphics       = TRUE,/* Control if running in graphics/text mode. */
  96.     GlblFatalError       = FALSE, /* True if disaster in system - must quit! */
  97.     GlblPrintLogFile     = FALSE,    /* If TRUE everything goes to log file. */
  98.     GlblPointLenAux      = IG_POINT_DEFAULT_LENGTH;
  99.  
  100. RealType
  101.     GlblPointLen = 0.02;               /* Scaler for point if drawn. */
  102.  
  103. char
  104. #ifdef DJGCC                       /* Defaults for intr_lib. */
  105.     *GlblViewWndwPos   = "0.02, 0.02, 0.72, 0.66",
  106.     *GlblTransWndwPos  = "0.75, 0.02, 0.98, 0.66",
  107.     *GlblStatusWndwPos = "0.75, 0.02, 0.98, 0.66",
  108.     *GlblInputWndwPos  = "0.04, 0.7,  0.98, 0.98",
  109.     *GlblIntrSaveDisk  = "c:\\",
  110. #endif /* DJGCC */
  111.     *GlblStartFileName = "",
  112.     *GlblLogFileName = "",
  113. #ifdef DOUBLE
  114.     *GlblFloatFormat = "%-8.6lg";
  115. #else
  116.     *GlblFloatFormat = "%-8.6g";
  117. #endif /* DOUBLE */
  118.  
  119. static ConfigStruct SetUp[] =
  120. {
  121. #ifdef DJGCC
  122.   { "Joystick",        (VoidPtr) &GlblJoystickExists,    SU_BOOLEAN_TYPE },
  123.   { "MouseSensitivity",    (VoidPtr) &GlblMouseSensitivity,SU_INTEGER_TYPE },
  124.   { "WndwWidth",    (VoidPtr) &GlblWindowFrameWidth,SU_INTEGER_TYPE },
  125.   { "WndwHeader",    (VoidPtr) &GlblDrawHeader,    SU_BOOLEAN_TYPE },
  126.   { "WndwSmthTxtScrl",    (VoidPtr) &GlblSmoothTextScroll,SU_BOOLEAN_TYPE },
  127.   { "WndwViewClr",    (VoidPtr) &GlblViewFrameColor,    SU_INTEGER_TYPE },
  128.   { "WndwTransClr",    (VoidPtr) &GlblTransFrameColor,    SU_INTEGER_TYPE },
  129.   { "WndwInputClr",    (VoidPtr) &GlblInputFrameColor,    SU_INTEGER_TYPE },
  130.   { "WndwViewPos",    (VoidPtr) &GlblViewWndwPos,    SU_STRING_TYPE },
  131.   { "WndwTransPos",    (VoidPtr) &GlblStatusWndwPos,    SU_STRING_TYPE },
  132.   { "WndwInputPos",    (VoidPtr) &GlblInputWndwPos,    SU_STRING_TYPE },
  133.   { "WndwBackSave",    (VoidPtr) &GlblIntrSaveMethod,    SU_INTEGER_TYPE },
  134.   { "WndwBackSavePath",    (VoidPtr) &GlblIntrSaveDisk,    SU_STRING_TYPE },
  135. #endif /* DJGCC */
  136.   { "DoGraphics",    (VoidPtr) &GlblDoGraphics,    SU_BOOLEAN_TYPE },
  137.   { "PointLength",    (VoidPtr) &GlblPointLenAux,    SU_INTEGER_TYPE },
  138.   { "LoadColor",    (VoidPtr) &GlblLoadColor,    SU_INTEGER_TYPE },
  139.   { "BoolColor",    (VoidPtr) &GlblBoolColor,    SU_INTEGER_TYPE },
  140.   { "ICrvColor",    (VoidPtr) &GlblICrvColor,    SU_INTEGER_TYPE },
  141.   { "PrimColor",    (VoidPtr) &GlblPrimColor,    SU_INTEGER_TYPE },
  142.   { "StartFile",    (VoidPtr) &GlblStartFileName,    SU_STRING_TYPE },
  143.   { "LogFile",        (VoidPtr) &GlblLogFileName,    SU_STRING_TYPE },
  144.   { "FloatFrmt",    (VoidPtr) &GlblFloatFormat,    SU_STRING_TYPE },
  145. };
  146. #define NUM_SET_UP    (sizeof(SetUp) / sizeof(ConfigStruct))
  147.  
  148. static void Interact(void);
  149. static void PrintInptPrsrError(void);
  150.  
  151. /*****************************************************************************
  152. * Main routine - Read Parameter    line and do what you need...             *
  153. *****************************************************************************/
  154. void main(int argc, char **argv)
  155. {
  156.     char *FullPathStartFileName;
  157.  
  158.     IritCPUTime(TRUE);                     /* Reset the clock. */
  159.  
  160.     Config("irit", SetUp, NUM_SET_UP);       /* Read config. file if exists. */
  161.  
  162.     while (argc >= 2) {
  163.         if (strncmp(argv[1], "-z", 2) == 0) {
  164.         fprintf(stderr, "\n%s\n\nUsage: %s\n", VersionStr, CtrlStr);
  165.         ConfigPrint(SetUp, NUM_SET_UP);
  166.         exit(0);
  167.         }
  168.     else if (strncmp(argv[1], "-t", 2) == 0) {
  169.         GlblDoGraphics = FALSE;
  170.     }
  171.     else {
  172.         break;
  173.     }
  174.         argv++;
  175.     argc--;
  176.     }
  177.  
  178.     getcwd(GlblCrntWorkingDir, LINE_LEN - 1);
  179.  
  180.     SetUpCtrlBrk();         /* Set up control break trap routine (int 1bh). */
  181.     signal(SIGFPE, DefaultFPEHandler);     /* Will trap floating point errors. */
  182.  
  183.     CagdSetFloatFormat(GlblFloatFormat);
  184.     IritPrsrSetFloatFormat(GlblFloatFormat);
  185.     IritPrsrSetFlattenObjects(FALSE);
  186.     IritPrsrSetPolyListCirc(TRUE);
  187.  
  188.     if (GlblDoGraphics) {
  189. #    ifdef DJGCC
  190.         GGSetStatusInputWindows(FALSE, TRUE);
  191.         GGInitGraph(0, FALSE);
  192. #    else
  193.         SocServerCreateSocket();
  194. #       endif /* DJGCC */
  195.     }
  196.  
  197.     /* Print some copyright messages: */
  198.     WndwInputWindowPutStr(GlblPrgmHeader);
  199.     WndwInputWindowPutStr(GlblCopyRight);
  200.     WndwInputWindowPutStr(GlblAuthorName);
  201.  
  202.     SetUpPredefObjects();          /* Prepare the predefined objects. */
  203.  
  204.     /* Execute the file specified in the command line if was one: */
  205.     if (argc == 2)
  206.     FileInclude(argv[1]);
  207.  
  208.     /* Execute the start up file first by inserting it to the include stack. */
  209.     if ((int) strlen(GlblStartFileName) > 0 &&
  210.     (FullPathStartFileName = searchpath(GlblStartFileName)) != NULL)
  211.     FileInclude(FullPathStartFileName);
  212.  
  213.     Interact();                      /* Go and do some real work... */
  214.  
  215.     IritExit(0);
  216. }
  217.  
  218. /*****************************************************************************
  219. * Interact - the main read/eval/print routine. This routine reads data from  *
  220. * standart input and execute it for ever (using Input Parser).             *
  221. * Note exit from this program is controled by input parser itself.         *
  222. *****************************************************************************/
  223. static void Interact(void)
  224. {
  225.     /* setjmp return 0 on first install time. Will return 1 if error is      */
  226.     /* recoverable, 2 if cannt continue - must quit the program now.         */
  227.     switch (setjmp(GlblLongJumpBuffer)) {         /* Used in error recovery. */
  228.     case 0:
  229.     case 1:
  230.         while (TRUE) {
  231.         if (!InputParser())         /* Print the error message. */
  232.             PrintInptPrsrError();
  233.         }
  234.     case 2:
  235.         WndwInputWindowPutStr("Press return to die...");
  236.         getchar();
  237.         break;
  238.     }
  239. }
  240.  
  241. /*****************************************************************************
  242. * Routine to query (and print) the errors found in InputParser:             *
  243. *****************************************************************************/
  244. static void PrintInptPrsrError(void)
  245. {
  246.     InptPrsrEvalErrType ErrorNum;
  247.     char *ErrorMsg, *p;
  248.     char Line[LINE_LEN_LONG];
  249.  
  250.     if ((ErrorNum = InptPrsrParseError(&ErrorMsg)) != IPE_NO_ERR) {/*Prsr err*/
  251.     sprintf(Line, "Parsing Error: ");
  252.     p = &Line[strlen(Line)];
  253.     switch (ErrorNum) {
  254.         case IP_ERR_WRONG_SYNTAX:
  255.         sprintf(p, "Wrong syntax\n");
  256.         break;
  257.         case IP_ERR_PARAM_EXPECT:
  258.         sprintf(p, "Parameter Expected\n");
  259.         break;
  260.         case IP_ERR_ONE_OPERAND:
  261.         case IP_ERR_TWO_OPERAND:
  262.         sprintf(p, "Wrong # of operands - %s\n", ErrorMsg);
  263.         break;
  264.         case IP_ERR_STACK_OV:
  265.         sprintf(p, "Internal Stack OverFlow at - %s\n", ErrorMsg);
  266.         break;
  267.         case IP_ERR_PARAM_MATCH:
  268.         sprintf(p, "Parenthesis mismatch - %s\n", ErrorMsg);
  269.         break;
  270.         case IP_ERR_UNDEF_TOKEN:
  271.         sprintf(p, "Undefined token - %s\n", ErrorMsg);
  272.         break;
  273.         case IP_ERR_UNDEF_FUNC:
  274.         sprintf(p, "Undefined function - %s\n", ErrorMsg);
  275.         break;
  276.         case IP_ERR_NAME_TOO_LONG:
  277.         sprintf(p, "Object name too long - %s\n", ErrorMsg);
  278.         break;
  279.         case IP_ERR_PARAM_FUNC:
  280.         sprintf(p, "Parameters expected in func %s\n", ErrorMsg);
  281.         break;
  282.         case IP_ERR_NO_PARAM_FUNC:
  283.         sprintf(p, "No Parameters expected in func %s\n", ErrorMsg);
  284.         break;
  285.         case IP_ERR_STR_TOO_LONG:
  286.         sprintf(p, "String too long - %s\n", ErrorMsg);
  287.         break;
  288.         default:
  289.         sprintf(p, "Undefined error %d", ErrorNum);
  290.         break;
  291.     }
  292.     WndwInputWindowPutStr(Line);
  293.     return;
  294.     }
  295.  
  296.     if ((ErrorNum = InptPrsrEvalError(&ErrorMsg)) != IPE_NO_ERR) {/*Eval err.*/
  297.     sprintf(Line, "Eval Error: ");
  298.     p = &Line[strlen(Line)];
  299.     switch (ErrorNum) {
  300.         case IE_ERR_FATAL_ERROR:
  301.         sprintf(p, "Fatal error - %s\n", ErrorMsg);
  302.         break;
  303.         case IE_ERR_DIV_BY_ZERO:
  304.         sprintf(p, "Division by zero - %s\n", ErrorMsg);
  305.         break;
  306.         case IE_ERR_NO_OBJ_METHOD:
  307.         sprintf(p, "No such method for object - %s\n", ErrorMsg);
  308.         break;
  309.         case IE_ERR_TYPE_MISMATCH:
  310.         sprintf(p, "Parameter type mismatch - %s\n",
  311.             ErrorMsg);
  312.         break;
  313.         case IE_ERR_ASSIGN_LEFT_OP:
  314.         sprintf(p, "Lval is not a parameter - %s\n", ErrorMsg);
  315.         break;
  316.         case IE_ERR_MIXED_OBJ:
  317.         sprintf(p, "Mixed types in expression - %s\n", ErrorMsg);
  318.         break;
  319.         case IE_ERR_IP_OBJ_UNDEFINED:
  320.         sprintf(p, "No such object defined - %s\n", ErrorMsg);
  321.         break;
  322.         case IE_ERR_NO_ASSIGNMENT:
  323.         sprintf(p, "Assignment was expected\n");
  324.         break;
  325.         case IE_ERR_FP_ERROR:
  326.         sprintf(p, "Floating Point Error - %s\n", ErrorMsg);
  327.         break;
  328.         case IE_ERR_NUM_PRM_MISMATCH:
  329.         sprintf(p, "Number of func. param. mismatch - %s\n", ErrorMsg);
  330.         break;
  331.         case IE_ERR_MAT_POWER:
  332.         sprintf(p, "Wrong range or not exists, operator - %s\n", ErrorMsg);
  333.         break;
  334.         case IE_ERR_FREE_SIMPLE:
  335.         sprintf(p, "Free only geometric objects - %s\n", ErrorMsg);
  336.         break;
  337.         case IE_ERR_MODIF_ITER_VAR:
  338.         sprintf(p, "Iteration var. type modified or freed - %s\n", ErrorMsg);
  339.         break;
  340.         case IE_ERR_BOOLEAN_ERR:
  341.         sprintf(p, "Geometric Boolean operation error - %s\n", ErrorMsg);
  342.         break;
  343.         case IE_ERR_OUT_OF_RANGE:
  344.         sprintf(p, "Out of range.\n");
  345.         break;
  346.         case IE_ERR_DATA_PRSR_ERROR:
  347.         sprintf(p, "%s", ErrorMsg);
  348.         break;
  349.         case IE_ERR_USER_FUNC_NO_RETVAL:
  350.         sprintf(p, "User defined function \"%s\" has no returned value\n",
  351.             ErrorMsg);
  352.         break;
  353.         case IE_ERR_INCOMPARABLE_TYPES:
  354.         sprintf(p, "Incomparable object types found");
  355.         break;
  356.         case IE_ERR_ONLYEQUALITY_TEST:
  357.         sprintf(p, "Only equality or non equality test is valid for these objects");
  358.         break;
  359.         case IE_ERR_IF_HAS_NO_COND:
  360.         sprintf(p, "Condition of if clause is illegal");
  361.         break;
  362.         case IE_ERR_IP_USERFUNC_DUP_VAR:
  363.         sprintf(p, "Duplicated variable, %s", ErrorMsg);
  364.         break;
  365.         default:
  366.         sprintf(p, "Undefined error %d\n", ErrorNum);
  367.         break;
  368.     }
  369.     WndwInputWindowPutStr(Line);
  370.     return;
  371.     }
  372. }
  373.  
  374. /*****************************************************************************
  375. * Exit routine with a return code of 0.                         *
  376. *****************************************************************************/
  377. void IritExit0(void)
  378. {
  379.     IritExit(0);
  380. }
  381.  
  382. /*****************************************************************************
  383. * Exit routine - called when this program should terminate.             *
  384. *****************************************************************************/
  385. void IritExit(int ExitCode)
  386. {
  387.     if (GlblDoGraphics) {
  388.         WndwViewExit();
  389.     }
  390.  
  391.     chdir(GlblCrntWorkingDir);      /* Recover original directory before exit. */
  392.  
  393.     if (GlblPrintLogFile)
  394.     fclose(GlblLogFile);              /* Close log file if was open. */
  395.  
  396.     exit(ExitCode);
  397. }
  398.  
  399. /*****************************************************************************
  400. * Trap Cagd_lib errors right here.                         *
  401. *****************************************************************************/
  402. void CagdFatalError(CagdFatalErrorType ErrID)
  403. {
  404.     char Line[LINE_LEN],
  405.     *ErrorMsg = CagdDescribeError(ErrID);
  406.  
  407.     sprintf(Line, "CAGD_LIB: %s", ErrorMsg);
  408.     WndwInputWindowPutStr(Line);
  409.  
  410.     longjmp(GlblLongJumpBuffer, 1);        /* Go back to menu directly. */
  411. }
  412.  
  413. /*****************************************************************************
  414. * FatalEror routine. print a fatal error message and go back to cursor mode  *
  415. *****************************************************************************/
  416. void IritFatalError(char *ErrorMsg)
  417. {
  418.     if (ErrorMsg != NULL) {
  419.     WndwInputWindowPutStr("Fatal error occured, please report it:");
  420.     WndwInputWindowPutStr(ErrorMsg);
  421.     }
  422.  
  423.     longjmp(GlblLongJumpBuffer, 1);        /* Go back to menu directly. */
  424. }
  425.  
  426. /*****************************************************************************
  427. *   Routine that is called from the floating point package in case of fatal  *
  428. * floating point error. Print error message, long jump to main loop. Default *
  429. * FPE handler - must be reset after redirected to other module.             *
  430. *****************************************************************************/
  431. void DefaultFPEHandler(int Type)
  432. {
  433.     char Line[LINE_LEN];
  434.  
  435.     sprintf(Line, "Floating point error %d.", Type);
  436.     WndwInputWindowPutStr(Line);
  437.  
  438.     longjmp(GlblLongJumpBuffer, 1);
  439. }
  440.  
  441. #define DEBUG
  442. #ifdef DEBUG
  443. void DummyLinkCagdDebug(void)
  444. {
  445.     CagdDbg(NULL);
  446. }
  447. #endif /* DEBUG */
  448.